#!/usr/bin/env python

#        +-----------------------------------------------------------------------------+
#        | GPL                                                                         |
#        +-----------------------------------------------------------------------------+
#        | Copyright (c) Mattia Lambertini <lamberti@cs.unibo.it>                      |
#        |                                                                             |
#        | This program is free software; you can redistribute it and/or               |
#        | modify it under the terms of the GNU General Public License                 |
#        | as published by the Free Software Foundation; either version 2              |
#        | of the License, or (at your option) any later version.                      |
#        |                                                                             |
#        | This program is distributed in the hope that it will be useful,             |
#        | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
#        | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
#        | GNU General Public License for more details.                                |
#        |                                                                             |
#        | You should have received a copy of the GNU General Public License           |
#        | along with this program; if not, write to the Free Software                 |
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
#        +-----------------------------------------------------------------------------+

# import xml.dom.minidom
import pickle
import anydbm
from time import gmtime, strftime
from __init__ import VARIABLES
import postit

__author__="mattia"
__date__ ="$Aug 24, 2009 8:04:17 PM$"

class PostitList:
    """
    
    """

    def __init__(self):
        self.postit_list = self.get_all_postits()

    def get_all_postits(self):
        """ """
        db = anydbm.open(VARIABLES['DB_NAME'], 'c')
        postits = []
        for key in db:
            content = pickle.loads(db[key])
            p = postit.Postit(key, self, content)
            postits.append(p)
        db.close()
        return postits

    def add_postit(self, content=""):
        """ """
        
        db = anydbm.open(VARIABLES['DB_NAME'], 'c')
        key = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
        db[key] = pickle.dumps(content)
        db.close()
        p = postit.Postit(key, self, content)
        self.postit_list.append(p)


    def __len__(self):
        return len(self.postit_list)


def update_postit(key, newContent, filename):
    """ """
    db = anydbm.open(filename, 'c')
    db[key] = pickle.dumps(newContent)
    db.close()

def delete_postit(key):
    """ """
    db = anydbm.open(VARIABLES['DB_NAME'], 'c')
    del db[key]
    db.close()

if __name__ == "__main__":
    print "postit_list.py DEBUG MODE";
    pl = PostitList()
    pl.add_postit(pl, "prima nota")

#    def __init__(self):
#        self.notes = self.__handle_notes()
#        self.postit_list = self.__get_all_postits()
#
#    def __handle_notes(self):
#        """ Return a list of notes"""
#        notes = []
#        try:
#            doc = xml.dom.minidom.parse("/home/mattia/NetBeansProjects/loveNote/src/data/notes.xml")
#            notes = doc.getElementsByTagName("note")
#        except:
#            print "no file data/notes.xml found"
#
#        return notes
#
#    def __read_note_content(self, note):
#        """ return the text content of a note"""
#        cNodes = note._get_childNodes()
#        contentStr = ''
#        for e in cNodes:
#                 if e.nodeType == e.ELEMENT_NODE and e.localName == "content":
#                    contentStr = e.childNodes[0].nodeValue
#
#        return contentStr
#
#    def __get_all_postits(self):
#        postit_list = []
#        for note in self.notes:
#            content = self.__read_note_content(note)
#            id = note.getAttribute("id")
#            postit = Postit(id, content)
#            postit_list.append(postit)
#        return postit_list
#
#    def get_note_by_id(self, id):
#        """ return the note element identified by id"""
#        notes = self.__handle_notes()
#        searchedNote = None
#        for note in notes:
#            if note.getAttribute("id") == str(id):
#                searchedNote = note
#                break
#        return searchedNote
#
#    def update_postit(self, id, newContent,):
#        """ update the post-it content in the xml"""
#        note = self.get_note_by_id(id)
#        cNodes = note._get_childNodes()
#        # set the new content in the xml node
#        for e in cNodes:
#                 if e.nodeType == e.ELEMENT_NODE and e.localName == "content":
#                    e.childNodes[0].nodeValue = newContent
#                    break
#
#    def __len__(self):
#        return len(self.postit_list)

# main
#    pl = PostitList()
#    pl.update_postit(1, "nuovo contenuto")